package is.web.htgen.table;

import is.bean.entity.region.Region;
import is.bean.entity.region.RegionBeanRemote;
import is.bean.entity.region.RegionBeanRemoteHome;
import is.web.htgen.Locale;
import is.web.xdumper.client.XdumperHtml;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.FinderException;
import nullutil.NullUtil;

/**
 *
 * @author aspr0
 */
public class RegionTable
{
    private Collection<RegionBeanRemote> regions;
    private RegionBeanRemoteHome regionHome;
    private Region findExample;
    private int start;
    private int count;

    public void setFindExample(Region findExample)
    {
        this.findExample = findExample;
    }

    public void setRegionHome(RegionBeanRemoteHome regionHome)
    {
        this.regionHome = regionHome;
    }

    public void setRegions(Collection<RegionBeanRemote> regions)
    {
        this.regions = regions;
    }

    public void setStart(int start)
    {
        this.start = start;
    }

    public void setCount(int count)
    {
        this.count = count;
    }

    public String generate() throws RemoteException, FinderException
    {
        XdumperHtml xh = new XdumperHtml();

        String formTable =
                "<table>"
                + "<thead>"
                + "%s"
                + "%s"
                + "</thead>"
                + "<form method=\"post\">"
                + "<tbody>"
                + "%s"
                + "<tr><td colspan=\"5\">%s</td></tr>"
                + "</tbody>"
                + "</form>"
                + "</table>"
                + "%s";
        String header =
                "<tr>"
                + "<td><input type=\"checkbox\" onclick=\"xdtCheckAll(this)\"></td>"
                + "<td>Id</td>"
                + "<td>Name</td>"
                + "<td>Parent region</td>"
                + "<td>Tools</td>"
                + "</tr>";
        String findId = NullUtil.toString(findExample.getId());
        String findName = NullUtil.toString(findExample.getName());
        String findFormRow =
                "<tr>"
                + "<form method=\"get\">"
                + "<td></td>"
                + "<td><input type=\"text\" name=\"id\" value=\"" + findId + "\" class=\"find-id\"></td>"
                + "<td><input type=\"text\" name=\"name\" value=\"" + findName + "\" class=\"find-wide\"></td>"
                + "<td></td>"
                + "<td>"
                + "<input type=\"hidden\" name=\"act\" value=\"find\">"
                + "<input type=\"submit\" value=\"find\"></td>"
                + "</form>"
                + "</tr>";
        String body = "";

        Iterator<RegionBeanRemote> i = regions.iterator();
        int idNum = 0;

        while (i.hasNext())
        {
            Region currentDepartment = i.next().getData();

            Integer id = currentDepartment.getId();
            String name = currentDepartment.getName();
            Integer parentId = currentDepartment.getParentId();


            String departmentLinkCanvas = "<a href=\"../region?id=%s&act=retrieve\">%s</a>";
            String parentLink = (NullUtil.isPositiveInteger(parentId))
                    ? String.format(departmentLinkCanvas,
                                    parentId,
                                    regionHome.findByPrimaryKey(parentId).getData().getName())
                    : Locale.NULL_READABLE;

            String row =
                    "<tr onclick=\"toggleChecked('id" + idNum + "')\">"
                    + "<td>"
                    + "<input "
                    + "type=\"checkbox\" "
                    + "name=\"id" + idNum + "\" "
                    + "id=\"id" + idNum + "\" "
                    + "value=\"" + id + "\" "
                    + "onclick=\"toggleChecked('id" + idNum + "')\">"
                    + "</td>"
                    + "<td>" + id + "</td>"
                    + "<td>" + name + "</td>"
                    + "<td>" + parentLink + "</td>"
                    + "<td>"
                    + "<a href=\"?id=" + id + "&act=retrieve\" title=\"retrieve\">"
                    + "<image src=\"../img/document-open.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=update\" title=\"update\">"
                    + "<image src=\"../img/document-properties.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=copy\" title=\"copy\">"
                    + "<image src=\"../img/edit-copy.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=delete\" title=\"delete\">"
                    + "<image src=\"../img/edit-delete.png\" class=\"hMargin\"></a>"
                    + "</td>"
                    + "</tr>";
            body += row;

            idNum++;
        }

        String inputHidden = String.format(xh.INPUT_HIDDEN_IDS_COUNT, idNum);
        String inputCheck = String.format(xh.DIV_ACTION_INPUT_CHECKBOX,
                                          "dumpref", "dumpReferences", "1", "Dump references");
        String inputSubmitDump = String.format(xh.DIV_ACTION_INPUT, xh.INPUT_SUBMIT_DUMP);

        String inputsXd = inputHidden + inputCheck + inputSubmitDump;

        String pager = Pager.generateHtml(count, start);


        return String.format(formTable, header, findFormRow, body, inputsXd, pager);
    }
}
